define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/stock/index' + location.search,
                    add_url: 'stock/stock/add',
                    edit_url: 'stock/stock/edit',
                    del_url: 'stock/stock/del',
                    multi_url: 'stock/stock/multi',
                    import_url: 'stock/stock/import',
                    table: 'stock',
                }
            });

            var table = $("#table");
			
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
						{field: 'idcode', title: __('Idcode'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('持票人')},
						{field: 'depart_id', title: __('所在单位'), searchList: $.getJSON("stock/stock/depart")},
						{field: 'user.username', title: __('工号'), operate: 'LIKE'},
						{field: 'user.idcard', title: __('身份证号'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'lmoney', title: __('Lmoney'), operate: 'LIKE'},
                        {field: 'releasetime', title: __('Releasetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __("Status"), searchList: {"0":__('未审核'),"1":__('正常'),"2":__('转让中'),"3":__('转出中')}, formatter: Table.api.formatter.status},
						{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons:[
								{
									name: 'transferUser',
									title: __('转让'),
									text:__('转让'),
									classname: 'btn btn-xs btn-primary btn-dialog',
									url: 'stock/stock/transferUser',
									extend: 'data-area=\'["50%","35%"]\''
								},
								{
									name: 'flow',
									title: __('转出'),
									text:__('转出'),
									classname: 'btn btn-xs btn-warning btn-dialog',
									url: 'stock/stock/flow',
								}
								,
								{
									name: 'reissue',
									title: __('丢失补办'),
									text:__('丢失补办'),
									classname: 'btn btn-xs btn-info btn-dialog',
									url: 'stock/stock/reissue',
									extend: 'data-area=\'["50%","30%"]\''
								}
							],formatter: function (value, row, index) {
                                if(row.status != 1){
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
		recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'stock/stock/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'idcode', title: __('Idcode'), operate: 'LIKE'},
                        {field: 'uname', title: __('持票人')},
						{field: 'depart_id', title: __('所在单位'), searchList: $.getJSON("stock/stock/depart")},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'stock/stock/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'stock/stock/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
		census: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/stock/census' + location.search,
                    table: 'stock',
                }
            });

            var table = $("#table");
			
			//当表格数据加载完成时
			

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
				showToggle: false,
				showColumns: false,
				searchFormVisible: true,
				search: false,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate:false},
						{field: 'depart', title: __('所在单位'), searchList: $.getJSON("stock/stock/depart")},
                        {field: 'summoney', title: __('本金'), operate:false},
						{field: 'ratemoney', title: __('利息'), operate:false}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
		newlist: function(){
			// 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/stock/newlist' + location.search,
                    edit_url: 'stock/stock/edit',
                    del_url: 'stock/stock/del',
                    multi_url: 'stock/stock/multi',
                    table: 'stock',
                }
            });

            var table = $("#table");
			
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
						{field: 'idcode', title: __('Idcode'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('持票人')},
						{field: 'depart_id', title: __('所在单位'), searchList: $.getJSON("stock/stock/depart")},
						{field: 'user.username', title: __('工号'), operate: 'LIKE'},
						{field: 'user.idcard', title: __('身份证号'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'lmoney', title: __('Lmoney'), operate: 'LIKE'},
                        {field: 'releasetime', title: __('Releasetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __("Status"), searchList: {"0":__('未审核'),"1":__('正常'),"2":__('转让中'),"3":__('转出中')}, formatter: Table.api.formatter.status},
						{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons:[
								{
									name: 'review',
									title: __('审核'),
									text:__('审核'),
									classname: 'btn btn-xs btn-primary btn-ajax',
									confirm: '确定要通过审核吗？',
									refresh:true,
									url: 'stock/stock/review'
								}
							], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
		},
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
		users: function(){
			 // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/stock/users?id='+Config.id,
                }
            });

            var table = $("#table");
			
			// 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
				showToggle: false,
				showExport: false,
				showColumns: false,
				maintainSelected: true,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'username', title: __('工号')},
                        {field: 'nickname', title: __('姓名')},
                        {field: 'depart.name', title: __('单位')},
                        {field: 'status', title: __("Status"), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'selectmanager',
                                    text: '选择',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-check',
                                    click: function (options, row, button) {
                                        Fast.api.close(row);
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
		},
		flow: function(){
			 // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/stock/flow?ids='+Config.ids,
                }
            });

            var table = $("#table");
			
			//初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
				showToggle: false,
				showExport: false,
				showColumns: false,
				maintainSelected: true,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('单位名称'),align: 'left',formatter:function (value, row, index) {
                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                            }},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'selectmanager',
                                    text: '选择',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-check',
                                    click: function (options, row, button) {
										Fast.api.ajax({
											url: "stock/stock/doFlow",
											type: "post",
											data: {id:Config.ids, to_id: row.id},
										}, function (data, ret) {
											var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
											parent.Toastr.success(msg);
											parent.$("#table").bootstrapTable("refresh");		
											Fast.api.close();
										});
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons}
                    ]
                ]
            });

            //为表格绑定事件
            Table.api.bindevent(table);
		},
		transferuser: function(){
			Controller.api.bindevent();
			 // 初始化表格参数配置
            // Table.api.init({
                // extend: {
                    // index_url: 'stock/stock/transferUser?ids='+Config.ids,
                // }
            // });

            // var table = $("#table");
			
			//初始化表格
            // table.bootstrapTable({
                // url: $.fn.bootstrapTable.defaults.extend.index_url,
				// showToggle: false,
				// showExport: false,
				// showColumns: false,
				// maintainSelected: true,
                // columns: [
                    // [
                        // {field: 'id', title: 'ID'},
                        // {field: 'username', title: __('工号')},
                        // {field: 'nickname', title: __('姓名')},
                        // {field: 'depart.name', title: __('单位')},
                        // {field: 'status', title: __("Status"), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'operate', title: __('Operate'), table: table, buttons: [
                                // {
                                    // name: 'selectmanager',
                                    // text: '选择',
                                    // classname: 'btn btn-xs btn-info btn-click',
                                    // icon: 'fa fa-check',
                                    // click: function (options, row, button) {
										// Fast.api.ajax({
											// url: "stock/stock/doTransfer",
											// type: "post",
											// data: {id:Config.ids, to_uid: row.id},
										// }, function (data, ret) {
											// var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
											// parent.Toastr.success(msg);
											// parent.$("#table").bootstrapTable("refresh");		
											// Fast.api.close();
										// });
                                    // }
                                // }
                            // ],
                            // formatter: Table.api.formatter.buttons}
                    // ]
                // ]
            // });

            //为表格绑定事件
            // Table.api.bindevent(table);
		},
		reissue: function(){
			Controller.api.bindevent();
		},
        api: {
            bindevent: function () {
				$(document).on("click", ".btn-select-managers", function () {
                    var url = $(this).data("url");
					var title = $(this).attr("title");
                    parent.Fast.api.open(url, title, {
                        callback: function (data) {
                            $("#c-uname").val(data.nickname);
							$("#c-uid").val(data.id);
							$("#c-depart").val(data.depart_id);
							$("#c-sub_depart").val(data.sub_depart);
                        }
                    });
                });
				
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
